//
//  DDBalanceDetailsViewController.m
//  DingDongZP
//
//  Created by weily on 16/5/11.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBalanceDetailsViewController.h"
#import "DDNavigationBarPullView.h"
#import "CCUICreator.h"
#import "DDBalanceDetailCell.h"

@interface DDBalanceDetailsViewController ()

@property (nonatomic, strong) DDNavigationBarPullView *pullView;
@property (nonatomic, strong) UILabel *titleLab;

@end

@implementation DDBalanceDetailsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.pullView = [[DDNavigationBarPullView alloc] initWithFrame:self.view.frame];
    self.pullView.hidden = YES;
    [self.view addSubview:_pullView];
    
    UILabel *titleLab = [UILabel labelWithFrame:CGRectMake(0, 0, 180, 30) font:[UIFont systemFontOfSize:15.0] textColor:[UIColor whiteColor] text:nil];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.userInteractionEnabled = YES;
    self.titleLab = titleLab;
    self.navigationItem.titleView = titleLab;
    
    NSArray *titles = @[@"全部", @"支出", @"收入", @""];
    self.pullView.titles = titles;
    __weak typeof(self) weakSelf = self;
    self.pullView.seletedTitleBlock = ^(NSInteger idx) {
        NSString *title = titles[idx];
        [weakSelf updateTitleView:title];
        [weakSelf updateWithTypeIdx:idx];
    };
    
    [self.titleLab bk_whenTapped:^{
        
        if (![weakSelf.pullView isShowPullView]) {
            [weakSelf.pullView showPullView];
        } else {
            [weakSelf.pullView dismissPullView];
        }
        
    }];
    
    [self updateTitleView:titles[0]];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DDBalanceDetailCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - function
- (void)updateWithTypeIdx:(NSInteger)idx
{
    ///	TODO: 按类型重新加载数据
    //    [self reloadData];
}

- (void)updateTitleView:(NSString *)title
{
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:title];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0,title.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    if ([self.pullView isShowPullView]) {
        textAttachment.image = [UIImage imageNamed:@"pull_up"];
    } else {
        textAttachment.image = [UIImage imageNamed:@"pull_down"];
    }
    
    textAttachment.bounds = CGRectMake(5, 2, 9, 5);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:title.length];
    
    self.titleLab.attributedText = titleAttributed;
}

#pragma mark - table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDBalanceDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    ///	TODO: 为cell赋值
//    cell.typeLab.text =
//    cell.dateLab.text =
//    cell.moneyLab.text =
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

@end
